package com.yangxd.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.handler.BusinessException;
import com.yangxd.hospital.entity.User;
import com.yangxd.hospital.mapper.UserMapper;
import com.yangxd.hospital.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxd.response.ResultCode;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangxd
 * @since 2021-04-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查找
     *
     * @param userName
     * @return
     */
    @Override
    public User selectByName(String userName) {
        return this.baseMapper.selectByName(userName);
    }

    /**
     * 根据departmentId查找
     *
     * @param departmentId
     * @return
     */
    @Override
    public List<User> listByDepartmentId(Integer departmentId) {
        return this.baseMapper.listByDepartmentId(departmentId);
    }

    @Override
    public User getUserInfo() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, username);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 修改用户信息
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {
        this.baseMapper.updateById(user);
    }

    /**
     * 分页查询医生信息
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public IPage<User> findPageDoctors(Page<User> page, QueryWrapper<User> wrapper) {
        return this.baseMapper.findPageDoctors(page,wrapper);
    }

    /**
     * 删除医生信息
     *
     * @param userId
     */
    @Override
    public void deleteById(Integer userId) {
        this.baseMapper.deleteById(userId);
    }

    /**
     * 注册用户
     *
     * @param user
     */
    @Override
    public Integer saveUser(User user) {
//        String userName = user.getUserName();
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_name",userName);
//        Integer count = this.baseMapper.selectCount(queryWrapper);
//        if(count > 0) {
//            throw new BusinessException(ResultCode.USER_ACCOUNT_ALREADY_EXIST.getCode(),
//                    ResultCode.USER_ACCOUNT_ALREADY_EXIST.getMessage());
//        } else {
            user.setDeleted(0);
            this.baseMapper.insert(user);
            Integer userId = user.getUserId();

            return userId;


//        }

    }


}
